﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace SHomeWorkshop.LunarPresentation
{
    public class KeyValueStringTable
    {
        public KeyValueStringTable() { }

        /// <summary>
        /// 构造一个列表。可以使用key来取出对应的value。
        /// </summary>
        /// <param name="tableSource">
        /// 字符列表源应形如：
        /// key1:value1|key2:value2|
        /// 
        /// 如果传入null或长度为零的字符串，则仍会存在一个无内容的、不可访问的空表Table。
        /// </param>
        public KeyValueStringTable(string tableSource)
        {
            //这里不能用字段。
            this.TableSource = tableSource;
        }

        /// <summary>
        /// 形如：
        /// key1:value1|key2:value2|
        /// 的一个字符串。
        /// </summary>
        public string TableSource
        {
            set
            {
                if (value == null || value.Length <= 0) return;

                if (value.Contains("|") == false && value.Contains(":") == false)
                {
                    throw new Exception("给TableSource赋的字符串不符合要求：" + value +
                        "\r\n要求字符串形如：\r\nkey1:value1|key2:value2|");
                }

                table.Clear();

                char[] spliterFst = { '|' };
                char[] spliterSec = { ':' };

                string[] lines = value.Split(spliterFst, StringSplitOptions.RemoveEmptyEntries);

                if (lines.Length <= 0) return;

                foreach (string line in lines)
                {
                    string[] feilds = line.Split(spliterSec, StringSplitOptions.RemoveEmptyEntries);

                    if (feilds.Length == 1)
                    {
                        table.Add(feilds[0], "");
                    }
                    else if (feilds.Length == 2)
                    {
                        table.Add(feilds[0], feilds[1]);
                    }
                    else
                    {
                        throw new Exception("给TableSource赋的字符串不符合要求：" + value
                            + "\r\n要求字符串形如：\r\nkey1:value1|key2:value2|");
                    }
                }
            }
        }

        /// <summary>
        /// 正常情况下会返回形如：
        /// key1:value1|key2:value2|的字符串。
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (table == null || table.Count <= 0) return "<!--空表-->";

            StringBuilder sb = new StringBuilder();
            foreach (DictionaryEntry de in table)
            {
                sb.Append(de.Key + ":" + de.Value + "|");
            }

            return sb.ToString();
        }

        private Hashtable table = new Hashtable();

        public string this[string key]
        {
            get
            {
                return table[key] as string;
            }

            set
            {
                table[key] = value;
            }
        }

        public void Clear()
        {
            table.Clear();
        }

        public bool AddKeyValue(string key, string value)
        {
            if (key == null) return false;

            table.Add(key, value == null ? "" : value);
            return true;
        }

        public bool Remove(string key)
        {
            if (table.Contains(key))
            {
                table.Remove(key);
                return true;
            }
            else return false;
        }
    }
}
